package br.com.carser.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.annotations.IndexColumn;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import br.com.carser.dao.infra.BaseEntity;

/**
 * <B>Project: carser-web</B><BR>
 * 
 * Entity class that represents the User.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Sep 18, 2011
 * @version 1.0
 */
@Entity
@Table(name = "tb_user")
public class User extends BaseEntity implements Comparable<User> {

	/**
	 * Serial version id.
	 */
	private static final long serialVersionUID = -6277786251060507121L;

	@Id
	@Column(name = "id_user", unique = true, precision = 3)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "cl_name", length = 50)
	@NotNull
	@NotEmpty
	@Pattern(regexp = "[a-zA-z]+", message = "{validation.invalid.user.name}")
	private String name;

	@Column(name = "cl_username", unique = true, nullable = false, length = 15)
	@NotNull
	@NotEmpty
	@Pattern(regexp = "[a-zA-z0-9_.]+", message = "{validation.invalid.user.username}")
	@Size(min = 5, max = 15, message = "{validation.size.user.username}")
	private String username;

	@Column(name = "cl_password", nullable = false, length = 15)
	@NotNull
	@NotEmpty
	@Size(min = 5, max = 15, message = "{validation.size.user.password}")
	private String password;

	@ManyToOne(targetEntity = Profile.class)
	@JoinColumn(name = "id_profile")
	@Valid
	private Profile profile;

	@ElementCollection
	@IndexColumn(name = "cl_index", nullable = false)
	@CollectionTable(name = "tb_email", joinColumns = { @JoinColumn(name = "id_user") })
	@Column(name = "cl_email")
	private List<String> emails = new ArrayList<String>();

	@ElementCollection
	@IndexColumn(name = "cl_index", nullable = false)
	@CollectionTable(name = "tb_phone", joinColumns = { @JoinColumn(name = "id_user") })
	@Column(name = "cl_phone")
	private List<String> phones = new ArrayList<String>();

	@Override
	public Long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @param profile
	 *            the profile to set
	 */
	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	/**
	 * @return the emails
	 */
	public List<String> getEmails() {
		return emails;
	}

	/**
	 * Add a email to user email list.
	 * 
	 * @param email
	 */
	public void addEmail(@Email String email) {
		emails.add(email);
	}

	/**
	 * Remove email
	 * 
	 * @param email
	 * @return true if removed
	 */
	public boolean removeEmail(String email) {
		return emails.remove(email);
	}

	/**
	 * @return the phones
	 */
	public List<String> getPhones() {
		return phones;
	}

	/**
	 * Add a phone number to user.
	 * 
	 * @param phone
	 */
	public void addPhone(String phone) {
		phones.add(phone);
	}

	/**
	 * Remove phone number.
	 * 
	 * @param phone
	 * @return true if number removed.
	 */
	public boolean removePhone(String phone) {
		return phones.remove(phone);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof User)) {
			return false;
		}
		User other = (User) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (password == null) {
			if (other.password != null) {
				return false;
			}
		} else if (!password.equals(other.password)) {
			return false;
		}
		if (username == null) {
			if (other.username != null) {
				return false;
			}
		} else if (!username.equals(other.username)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [username=" + username + ", username=" + username + "]";
	}

	@Override
	public int compareTo(User o) {
		return this.username.compareTo(o.username);
	}

}
